#include "wtlpch.h"

#include <time.h>

#include "resource.h"
#include "FileDownInfoList.h"

#define INFOLIST_TIMESTAMP_INDEX	0
#define INFOLIST_INFO_INDEX			1

#define INFO_HEADER_MAX_LENGTH			16
#define INFO_TIMESTAMP_COLUMN_WIDTH		120
#define INFO_TIMESTRING_MAX_WIDTH		64

#define NORMAL_INFO_COLOR			RGB(230, 230, 250)
#define ERROR_INFO_COLOR			RGB(255, 192, 253)
#define SUCCESS_INFO_COLOR			RGB(180, 251, 190)
#define OTHER_INFO_COLOR			RGB(175, 238, 238)

CFileDownInfoList::CFileDownInfoList() : m_uiListItemIndex(0)
{
}

CFileDownInfoList::~CFileDownInfoList()
{
}

void CFileDownInfoList::ShowInfo(INFO_STATUS infoStatus, LPCWSTR lpcwstrInfo)
{
	//Generate the time
	__time32_t ttTimeNow;
	struct tm* ptmTimeNow;
	WCHAR pwstrTimeString[INFO_TIMESTRING_MAX_WIDTH] = { 0 };
	
	::_time32(&ttTimeNow);
	ptmTimeNow = ::_localtime32(&ttTimeNow);
	
	::wcsftime(pwstrTimeString, INFO_TIMESTRING_MAX_WIDTH, L"%Y-%m-%d %H:%M:%S", ptmTimeNow);

	AddItem(m_uiListItemIndex, INFOLIST_TIMESTAMP_INDEX, pwstrTimeString);
	AddItem(m_uiListItemIndex, INFOLIST_INFO_INDEX, lpcwstrInfo);
	SetItemData(m_uiListItemIndex, (DWORD_PTR)infoStatus);

	SelectItem(m_uiListItemIndex);

	m_uiListItemIndex++;
}

LRESULT CFileDownInfoList::OnResizeColumn(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	//Adjust Column Size
	int nWidth = LOWORD(lParam);
	int nInfoColumnSize = nWidth - GetColumnWidth(INFOLIST_TIMESTAMP_INDEX);

	//Make the header to hold its space
	SetColumnWidth(INFOLIST_INFO_INDEX, nInfoColumnSize);

	return FALSE;
}

LRESULT CFileDownInfoList::OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	//Set Exstyle
	SetExtendedListViewStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);

	//Add Columns
	WCHAR lpwstrTitle[INFO_HEADER_MAX_LENGTH] = { 0 };

	::LoadStringW(_Module.m_hInst, IDS_TA_DOWNINFO_TIME_TITLE, lpwstrTitle, INFO_HEADER_MAX_LENGTH);
	InsertColumn(INFOLIST_TIMESTAMP_INDEX, lpwstrTitle, LVCFMT_LEFT, INFO_TIMESTAMP_COLUMN_WIDTH);

	::wmemset(lpwstrTitle, 0, INFO_HEADER_MAX_LENGTH);
	::LoadStringW(_Module.m_hInst, IDS_TA_DOWNINFO_INFO_TITLE, lpwstrTitle, INFO_HEADER_MAX_LENGTH);
	InsertColumn(INFOLIST_INFO_INDEX, lpwstrTitle, LVCFMT_LEFT, INFO_TIMESTAMP_COLUMN_WIDTH);

	bHandled = FALSE;
	return FALSE;
}

LRESULT CFileDownInfoList::OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	//Small trick for resize listview
	PostMessageW(WM_RESIZECOLUMN, NULL, lParam);

	bHandled = FALSE;
	return FALSE;
}

LRESULT CFileDownInfoList::OnCustomDrawColumn(int idCtrl, LPNMHDR lpnmhdr, BOOL& bHandled)
{
	//We only check msg from self
	if(lpnmhdr->hwndFrom == m_hWnd)
	{
		LPNMLVCUSTOMDRAW pLVCD = (LPNMLVCUSTOMDRAW)lpnmhdr;

		if (CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage)
		{
			//We want to receive msg for every item
			return CDRF_NOTIFYITEMDRAW;
		}
		else if(CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage)
		{
			//We want to receive msg for every subitem
			//We have to do this in two stage cause of MSDN
			return CDRF_NOTIFYSUBITEMDRAW;
		}
		else if((CDDS_ITEMPREPAINT | CDDS_SUBITEM) == pLVCD->nmcd.dwDrawStage)
		{
			//We only want to change the color of info column
			if(pLVCD->iSubItem == INFOLIST_INFO_INDEX)
			{
				switch((INFO_STATUS)GetItemData(pLVCD->nmcd.dwItemSpec))
				{
				case ST_INFO:

					pLVCD->clrTextBk = NORMAL_INFO_COLOR;
					break;

				case ST_OK:

					pLVCD->clrTextBk = SUCCESS_INFO_COLOR;
					break;

				case ST_ERROR:
				
					pLVCD->clrTextBk = ERROR_INFO_COLOR;
					break;

				case ST_OTHER:
					
					pLVCD->clrTextBk = OTHER_INFO_COLOR;
					break;
				}
			}

			return CDRF_DODEFAULT;
		}
			
		return CDRF_DODEFAULT;
	}

	//Pass other window's message out
	bHandled = FALSE;
	return FALSE;
}